﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WatchMyToon
{
    public partial class ConfigForm : Form
    {
        private configloader confl;
        string file = Application.StartupPath + "\\Plugins\\WatchMyToon\\config.ini";

        public ConfigForm()
        {
            InitializeComponent();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                FTPServer.Enabled = true;
                FTPUserName.Enabled = true;
                FTPUserPassword.Enabled = true;
            }
            else
            {
                FTPServer.Enabled = false;
                FTPUserName.Enabled = false;
                FTPUserPassword.Enabled = false;
            }

        }

        private void Config_Load(object sender, EventArgs e)
        {
            confl = new configloader(file);
            confl.load();

            string EnableFTP = confl.get("EnableFTP");
            string _FTPServer = confl.get("FTPServer");
            string _FTPUserName = confl.get("FTPUserName");
            string _FTPUserPassword = confl.get("FTPUserPassword");


            if (string.IsNullOrEmpty(EnableFTP))
            {
                checkBox1.Checked = false;
                FTPServer.Enabled = false;
                FTPUserName.Enabled = false;
                FTPUserPassword.Enabled = false;
            }
            else
            {
                if (EnableFTP.Equals("true"))
                {
                    checkBox1.Checked = true;
                    FTPServer.Enabled = true;
                    FTPUserName.Enabled = true;
                    FTPUserPassword.Enabled = true;

                    FTPServer.Text = _FTPServer;
                    FTPUserName.Text = _FTPUserName;
                    FTPUserPassword.Text = _FTPUserPassword;
                }
                else
                {
                    checkBox1.Checked = false;
                    FTPServer.Enabled = false;
                    FTPUserName.Enabled = false;
                    FTPUserPassword.Enabled = false;

                    FTPServer.Text = _FTPServer;
                    FTPUserName.Text = _FTPUserName;
                    FTPUserPassword.Text = _FTPUserPassword;
                }
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                if (string.IsNullOrEmpty(FTPServer.Text))
                {
                    MessageBox.Show("Please enter a FTPServer", "Info");
                    FTPServer.Focus();

                }
                else if (string.IsNullOrEmpty(FTPUserName.Text))
                {
                    MessageBox.Show("Please enter a FTPUserName", "Info");
                    FTPUserName.Focus();

                }
                else if (string.IsNullOrEmpty(FTPUserPassword.Text))
                {
                    MessageBox.Show("Please enter a FTPUserPassword", "Info");
                    FTPUserPassword.Focus();

                }
                else
                {
                    confl.set("EnableFTP", true.ToString());
                    confl.set("FTPServer", FTPServer.Text);
                    confl.set("FTPUserName", FTPUserName.Text);
                    confl.set("FTPUserPassword", FTPUserPassword.Text);
                }

                
                this.Close();

            }
            else
            {
                confl.set("EnableFTP", false.ToString());
                confl.set("FTPServer", FTPServer.Text);
                confl.set("FTPUserName", FTPUserName.Text);
                confl.set("FTPUserPassword", FTPUserPassword.Text);

                this.Close();
            }
        }
    }
}
